package com.wzgl.core.utils;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.log.Log;

/**
 * Created by MRFMRF on 2016/6/2.
 */
public class ResultInfoUtils {

    private static Log logger=Log.getLog(ResultInfoUtils.class);
    private static StackTraceElement[] parentClasses;

    private static String retCodeKey = "error";
    private static String retMessKey = "message";
    private static String errorNumKey = "errorNum";
    private static String dataKey = "result";

    private static Map<String, Object> resultMap = new HashMap<String, Object>();

    public static String infoData(int retCode, String message) {
        parentClasses = Thread.currentThread().getStackTrace();
        resultMap.clear();
        resultMap.put(retCodeKey, retCode);
        resultMap.put(retMessKey, message);
        
        String result = JSONObject.toJSONString(resultMap);

        logger.info("[类" + parentClasses[2].getClassName() + "方法"
                + parentClasses[2].getMethodName() + "第"
                + parentClasses[2].getLineNumber() + "行] - " + result);

        return result;
    }

    public static String infoData(int retCode, String message, Object dataObj) {
        parentClasses = Thread.currentThread().getStackTrace();
        resultMap.clear();
        resultMap.put(retCodeKey, retCode);
        resultMap.put(retMessKey, message);
        resultMap.put(dataKey, dataObj);

        String result = JSONObject.toJSONString(resultMap);

        logger.info("[类" + parentClasses[2].getClassName() + "方法"
                + parentClasses[2].getMethodName() + "第"
                + parentClasses[2].getLineNumber() + "行] - " + result);

        return result;
    }

    public static String errorData(int retCode, String message) {
        String errorNum = String.valueOf(System.currentTimeMillis()) + new Random().nextInt(1000);
        return errorData(errorNum, retCode, message);
    }

    public static String errorData(int retCode, String message, Throwable cause) {
        parentClasses = cause.getStackTrace();
        String errorNum = String.valueOf(System.currentTimeMillis()) + new Random().nextInt(1000);

        resultMap.clear();
        resultMap.put(retCodeKey, retCode);
        resultMap.put(retMessKey, message);
        resultMap.put(errorNumKey, errorNum);

        String result = JSONObject.toJSONString(resultMap);

        for (StackTraceElement stackTraceElement : parentClasses) {
            if (stackTraceElement.getClassName().contains("com.kingold")) {

                logger.info("[错误编号: " + errorNum + "][类" + stackTraceElement.getClassName() + "方法"
                        + stackTraceElement.getMethodName() + "第"
                        + stackTraceElement.getLineNumber() + "行] - " + result);
            }
        }

        return result;
    }

    public static String errorData(String errorNum, int retCode, String message) {
        parentClasses = Thread.currentThread().getStackTrace();
        resultMap.clear();
        resultMap.put(retCodeKey, retCode);
        resultMap.put(retMessKey, message);
        resultMap.put(errorNumKey, errorNum);

        String result = JSONObject.toJSONString(resultMap);
        logger.info("[错误编号: " + errorNum + "][类" + parentClasses[2].getClassName() + "方法"
                + parentClasses[2].getMethodName() + "第"
                + parentClasses[2].getLineNumber() + "行] - " + result);

        return result;
    }
    public static String result(boolean b){
    	parentClasses = Thread.currentThread().getStackTrace();
        resultMap.clear();
		int status = 0;
		String content = "操作成功!";
		if(!b){
			status = 1;
			content = "操作失败,请稍后再试!";
		}
		resultMap.put(retCodeKey, status);
		resultMap.put(retMessKey, content);
		String results = JSONObject.toJSONString(resultMap);
		return results;
	}
}
